package com.indexingsystem.system.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.admin.mapper.InstitutionsMapper;
import com.indexingsystem.boss.admin.mapper.PusersMapper;
import com.indexingsystem.boss.admin.mapper.UserInfosMapper;
import com.indexingsystem.boss.admin.mapper.UserInstitutionMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.UserInstitution;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.service.IUserInstitutionService;

@Service
public class UserInstitutionServiceImpl implements IUserInstitutionService {

	@Resource
	private UserInstitutionMapper userInstitutionMapper;

	@Resource
	private PusersMapper pusersMapper;

	@Resource
	private UserInfosMapper userInfosMapper;

	@Resource
	private InstitutionsMapper institutionsMapper;

	@Override
	public int deleteByPrimaryKey(String userInstitutionId) {
		return userInstitutionMapper.deleteByPrimaryKey(userInstitutionId);
	}

	/**
	 * 新增机构、用户、用户详情、机构用户关系新增操作.
	 */
//	@Override
//	public int insertSelective(UserInstitution record) throws Exception{
//		Pusers pusers = record.getPusers();
//		pusersMapper.insertUserSelective(pusers);
//		UserInfos userInfos = record.getUserInfos();
//		userInfosMapper.insertUserInfoSelective(userInfos);
//		Institutions institutions = record.getInstitutions();
//		institutionsMapper.insertSelective(institutions);
//		UserInstitution userInstitution = new UserInstitution();
//		userInstitution.setUserInstitutionId(record.getUserInstitutionId());
//		userInstitution.setInstitutionId(record.getInstitutionId());
//		userInstitution.setUserId(record.getUserId());
//		userInstitution.setRelation(record.getRelation());
//		return userInstitutionMapper.insertSelective(userInstitution);
//	}

	@Override
	public UserInstitution selectByPrimaryKey(String userInstitutionId) {
		return userInstitutionMapper.selectByPrimaryKey(userInstitutionId);
	}

	@Override
	public int updateByPrimaryKeySelective(UserInstitution record) {
		return userInstitutionMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public PagedResult<UserInstitution> selectUserInstitutionList(Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(userInstitutionMapper.selectUserInstitutionList());
	}

	/**
	 * 通过userId删除用户机构关系信息.
	 */
	@Override
	public int deleteUserInstitutionByUserId(String userId) {
		return userInstitutionMapper.deleteUserInstitutionByUserId(userId);
	}

	/**
	 * 通过userId查询用户机构关系.
	 * @param userId
	 * @return
	 */
	@Override
	public UserInstitution selectUserInstitutionByUserId(String userId) {
		return userInstitutionMapper.selectUserInstitutionByUserId(userId);
	}
}